package system_districts

import (
	"gitee.com/theegg/go-brick/app/service/app"
)

// 基础结构
type District struct {
	Label   string `json:"label"`
	LabelEn string `json:"label_en"`
	IsLand  string `json:"island"`
	Pid     int    `json:"pid"`
	Type    int    `json:"type"`
	Value   int    `json:"value"`
}

// 返回的数据结构 select
type DistrictSelectTree struct {
	Value    int64                 `json:"value"`
	Label    string                `json:"label"`
	LabelEn  string                `json:"label_en"`
	Children []*DistrictSelectTree `json:"children"`
}

var DistrictCountry []map[string]string = []map[string]string{
	0: map[string]string{
		"sort":    "1",
		"value":   "cn",
		"label":   "中国",
		"labelEn": "China",
	},
	1: map[string]string{
		"sort":    "2",
		"value":   "sg",
		"label":   "新加坡",
		"labelEn": "Singapore",
	},
	2: map[string]string{
		"sort":    "3",
		"value":   "tha",
		"label":   "泰国",
		"labelEn": "Thailand",
	},
	3: map[string]string{
		"sort":    "4",
		"value":   "ph",
		"label":   "菲律宾",
		"labelEn": "Philippines",
	},
	4: map[string]string{
		"sort":    "5",
		"value":   "vie",
		"label":   "越南",
		"labelEn": "Vietnam",
	},
}

func GetDistrictSortOptionByCountry(countryStr string) ([]*DistrictSelectTree, error) {
	districts, err := GetDistrictByCountryFromCache(countryStr)
	if err != nil {
		return nil, app.NewError(err)
	}

	data := BuildDistrictTree(districts, 0)
	return data, nil
}

// @title DistrictSelectTree
// @description 构建tree
func BuildDistrictTree(districts []*SystemDistricts, pid int64) []*DistrictSelectTree {
	data := make([]*DistrictSelectTree, 0)
	for _, item := range districts {
		if item.Pid == pid {
			tmpObj := DistrictSelectTree{
				Value: item.Id,
				Label: item.TitleCn,
			}
			tmpObj.Children = BuildDistrictTree(districts, item.Id)
			data = append(data, &tmpObj)
		}
	}

	return data
}

// 获取	District 对象
func GetDistrict(id int64) (*SystemDistricts, error) {
	districts, err := GetAllDistrictFromCache()
	if err != nil {
		return nil, app.NewError(err)
	}

	if _, ok := districts[id]; ok {
		return districts[id], nil
	}

	return nil, app.NewError("不存在的地区")
}

// 获取 GetDistrictName
func GetDistrictName(id int64) string {
	ds, err := GetDistrict(id)
	if err != nil {
		return "未知地区"
	}
	return ds.TitleCn
}

// 是否存在省份
func IsHasProvince(provinceId int64) bool {
	_, err := GetDistrict(provinceId)
	if err != nil {
		return false
	}

	return true
}

// 是否存在城市
func IsHasCity(provinceId int64, cityId int64) bool {
	ds, err := GetDistrict(cityId)
	if err != nil {
		return false
	}

	if ds.Pid != provinceId {
		return false
	}

	return true
}

// @title GetDistrictsByCountry
// @description 获取国家下的地区
func GetDistrictsByCountry(countryStr string) ([]*SystemDistricts, error) {

	items := make([]*SystemDistricts, 0)
	errPer := app.Db().Where("country = ?", countryStr).Asc("id").Find(&items)
	if errPer != nil {
		return nil, app.NewError(errPer)
	}

	return items, nil
}

// @title GetDistrictsByCountry
// @description 获取国家下的地区
func GetAllDistricts() (map[int64]*SystemDistricts, error) {

	items := make(map[int64]*SystemDistricts, 0)
	errPer := app.Db().Asc("id").Find(&items)
	if errPer != nil {
		return nil, app.NewError(errPer)
	}

	return items, nil
}
